package com.zb.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zb.entity.Car;
import com.zb.entity.User;
import com.zb.service.CarService;
import com.zb.service.UserService;
import com.zb.serviceimpl.CarServiceImpl;
import com.zb.serviceimpl.UserServiceImpl;
import com.zb.util.PageUtil;


public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
  
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		String op = request.getParameter("op");
		if(op.equals("show")) {
			this.doshow(request, response);
		}else if(op.equals("del")) {
			this.dodel(request, response);
		}else if(op.equals("toupdate")) {
			this.toupdate(request, response);
		}else if(op.equals("doupdate")) {
			this.doupdate(request, response);
		}
	}
	protected void doshow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserService userService=new UserServiceImpl();
		CarService c=new CarServiceImpl();
		int index=1;
		
		if (request.getParameter("index")!=null&&!"".equals(request.getParameter("index"))) {
			index = Integer.parseInt(request.getParameter("index"));
		}
			PageUtil<User> page = userService.getUserPage(index);
			request.setAttribute("mypage", page);
			List<Car> deptList = c.getCarList();
			request.setAttribute("dlist", deptList);
			
			request.getRequestDispatcher("search_kh.jsp").forward(request, response);
		
	}
	protected void dodel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		int nid= Integer.parseInt(request.getParameter("id"));
		UserService userService=new UserServiceImpl();
		userService.delUser(nid);
		response.sendRedirect("User.do?op=show");
	}
	protected void toupdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserService userService=new UserServiceImpl();
		CarService c=new CarServiceImpl();
		List<Car> dList = c.getCarList();
		request.setAttribute("dlist", dList);
		
		int id = Integer.parseInt(request.getParameter("id"));
		
		User p = userService.getUserById(id);
		request.setAttribute("p",p);
		request.getRequestDispatcher("userupdate.jsp").forward(request, response);
	}
	protected void doupdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserService userService=new UserServiceImpl();
		User u=new User();
		Car c=new Car();
		int id=Integer.parseInt(request.getParameter("id"));
		String name=request.getParameter("name");
		String sex=request.getParameter("sex");
		String tel=request.getParameter("tel");
		String mail=request.getParameter("mail");
		int carid=Integer.parseInt(request.getParameter("carid"));
		String day=request.getParameter("day");
		String address=request.getParameter("address");
		int score=Integer.parseInt(request.getParameter("score"));
		int money=Integer.parseInt(request.getParameter("money"));
		String loginname=request.getParameter("loginname");
		String loginpwd=request.getParameter("loginpwd");
		u.setId(id);
		u.setName(name);
		u.setSex(sex);
		u.setTel(tel);
		u.setEmail(mail);
		c.setId(carid);
		u.setDay(day);
		u.setAddress(address);
		u.setScore(score);
		u.setBalance(money);
		u.setLoginName(loginname);
		u.setLoginPwd(loginpwd);
		u.setCar(c);
		userService.update(u);
		
		response.sendRedirect("User.do?op=show");
		
	}
	}
		

